package com.pm.business.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.pm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pm.business.domain.CurrentProjectPresonMap;
import com.pm.business.mapper.CurrentProjectMapper;
import com.pm.business.domain.CurrentProject;
import com.pm.business.service.ICurrentProjectService;

/**
 * 项目基本资料Service业务层处理
 * 
 * @author zxl
 * @date 2022-05-05
 */
@Service
public class CurrentProjectServiceImpl implements ICurrentProjectService 
{
    @Autowired
    private CurrentProjectMapper currentProjectMapper;

    /**
     * 查询项目基本资料
     * 
     * @param projectId 项目基本资料主键
     * @return 项目基本资料
     */
    @Override
    public CurrentProject selectCurrentProjectByProjectId(Long projectId)
    {
        return currentProjectMapper.selectCurrentProjectByProjectId(projectId);
    }
    @Override
    public List<CurrentProject> selectCurrentProjectAwardNumberByProjectId(List<Long> projectIds)
    {
        return currentProjectMapper.selectCurrentProjectAwardNumberByProjectId(projectIds);
    }
    @Override
    public List<CurrentProject> selectCurrentProjectSysFileInfoNumberByProjectId(List<Long> projectIds)
    {
        return currentProjectMapper.selectCurrentProjectSysFileInfoNumberByProjectId(projectIds);
    }

    /**
     * 查询项目基本资料列表
     * 
     * @param currentProject 项目基本资料
     * @return 项目基本资料
     */
    @Override
    public List<CurrentProject> selectCurrentProjectList(CurrentProject currentProject)
    {
        return currentProjectMapper.selectCurrentProjectList(currentProject);
    }

    /**
     * 新增项目基本资料
     * 
     * @param currentProject 项目基本资料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCurrentProject(CurrentProject currentProject)
    {
        int rows = currentProjectMapper.insertCurrentProject(currentProject);
        insertCurrentProjectPresonMap(currentProject);
        return rows;
    }

    /**
     * 修改项目基本资料
     * 
     * @param currentProject 项目基本资料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCurrentProject(CurrentProject currentProject)
    {
        currentProjectMapper.deleteCurrentProjectPresonMapByProjectId(currentProject.getProjectId());
        insertCurrentProjectPresonMap(currentProject);
        return currentProjectMapper.updateCurrentProject(currentProject);
    }

    /**
     * 批量删除项目基本资料
     * 
     * @param projectIds 需要删除的项目基本资料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCurrentProjectByProjectIds(Long[] projectIds)
    {
        currentProjectMapper.deleteCurrentProjectPresonMapByProjectIds(projectIds);
        return currentProjectMapper.deleteCurrentProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目基本资料信息
     * 
     * @param projectId 项目基本资料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCurrentProjectByProjectId(Long projectId)
    {
        currentProjectMapper.deleteCurrentProjectPresonMapByProjectId(projectId);
        return currentProjectMapper.deleteCurrentProjectByProjectId(projectId);
    }

    @Override
    public List<CurrentProject> selectPersonProjectById(Long personId) {
        return currentProjectMapper.selectPersonProjectById(personId);
    }

    /**
     * 新增现存项目与人员关联信息
     * 
     * @param currentProject 项目基本资料对象
     */
    public void insertCurrentProjectPresonMap(CurrentProject currentProject)
    {
        List<CurrentProjectPresonMap> currentProjectPresonMapList = currentProject.getCurrentProjectPresonMapList();
        Long projectId = currentProject.getProjectId();
        if (StringUtils.isNotNull(currentProjectPresonMapList))
        {
            List<CurrentProjectPresonMap> list = new ArrayList<CurrentProjectPresonMap>();
            for (CurrentProjectPresonMap currentProjectPresonMap : currentProjectPresonMapList)
            {
                currentProjectPresonMap.setProjectId(projectId);
                list.add(currentProjectPresonMap);
            }
            if (list.size() > 0)
            {
                currentProjectMapper.batchCurrentProjectPresonMap(list);
            }
        }
    }
}
